package com.aquarium.ui.controller;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;

import com.aquarium.R;
import com.aquarium.data.ModuleSettingDataBase;
import com.aquarium.module.ModuleControlService;

public class WaveMotorSetting extends ControllerSetting {

	public static final String INTENT_EXTRA_WAVE_MOTOR_INDEX = "INTENT_EXTRA_WAVE_MOTOR_INDEX";

	public static final int WAVE_MOTOR_INDEX_1 = 1;
	public static final int WAVE_MOTOR_INDEX_2 = 2;

	private int mWaveMotorIndex;

	private String mTitleString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mTitle.setText(mTitleString);
	}

	@Override
	protected void subInit() {
		super.subInit();
		Intent intent = getIntent();
		mTitleString = intent
				.getStringExtra(ControllerSetting.INTENT_DATA_TITLE);
		if (getResources().getString(R.string.wave_motor1).equals(mTitleString)) {
			mWaveMotorIndex = WAVE_MOTOR_INDEX_1;
		} else if (getResources().getString(R.string.wave_motor2).equals(
				mTitleString)) {
			mWaveMotorIndex = WAVE_MOTOR_INDEX_2;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		invalidateListView();
	}

	@Override
	public void setListViewBinder() {

		mTimePickListViewAdapter
				.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
					@Override
					public boolean setViewValue(View view, Cursor cursor,
							int columnIndex) {
						boolean result = bindListView(view, cursor);
						int id = view.getId();
						if (id == R.id.wave_mode_txv) {
							String waveMode = mModuleSettingDataBase
									.getWaveMode(cursor);
							((TextView) view).setText(waveMode);
							return true;
						}
						return result;
					}
				});
	}

	@Override
	public void createListViewAdapter() {
		Cursor c = mModuleSettingDataBase.getWaveMotorCursor(mWaveMotorIndex);
		System.out.println("cursor count = " + c.getCount());
		mTimePickListViewAdapter = new TimePickListViewAdapter(this,
				R.layout.wave_moter_setting_row, c, new String[] {
						ModuleSettingDataBase.HOUR,
						ModuleSettingDataBase.MINUTE,
						ModuleSettingDataBase.WAVE_MODE,
						ModuleSettingDataBase.MON, ModuleSettingDataBase.TUES,
						ModuleSettingDataBase.WED, ModuleSettingDataBase.THUR,
						ModuleSettingDataBase.FRI, ModuleSettingDataBase.SAT,
						ModuleSettingDataBase.SUN }, new int[] {
						R.id.start_time_txv, R.id.wave_mode_txv, R.id.mon_txv,
						R.id.tues_txv, R.id.wed_txv, R.id.thur_txv,
						R.id.fri_txv, R.id.sat_txv, R.id.sun_txv });
	}

	@Override
	public void setListeners() {
		super.setListeners();
		mAddButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ACTION_ADD_SETTING);
				System.out.println("1....." + mWaveMotorIndex);
				intent.putExtra(INTENT_EXTRA_WAVE_MOTOR_INDEX, mWaveMotorIndex);
				intent.setClass(WaveMotorSetting.this,
						WaveMotorTimePicker.class);
				startActivity(intent);
			}
		});
		mDeleteButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View view) {
				mModuleSettingDataBase.deleteWaveMotorData(mDeleteList,
						mWaveMotorIndex);
				invalidateListView();
				Intent intent = new Intent();
				switch (mWaveMotorIndex) {
				case 1:
					intent = new Intent(
							ModuleControlService.ACTION_UPDATE_WAVE_MOTOR_1_DATA);
					break;
				case 2:
					intent = new Intent(
							ModuleControlService.ACTION_UPDATE_WAVE_MOTOR_2_DATA);
					break;
				default:
					break;
				}

				sendBroadcast(intent);
			}
		});

		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterview, View view,
					int position, long id) {
				Intent intent = new Intent(ACTION_MODIFY_SETTING);
				intent.putExtra(INTENT_EXTRA_WAVE_MOTOR_INDEX, mWaveMotorIndex);
				intent.putExtra(INTENT_EXTRA_LISTVIEW_CLICKED_POSITION,
						position);
				intent.setClass(WaveMotorSetting.this,
						WaveMotorTimePicker.class);
				startActivity(intent);
			}
		});
	}

	private void invalidateListView() {
		Cursor c = mModuleSettingDataBase.getWaveMotorCursor(mWaveMotorIndex);
		mTimePickListViewAdapter.changeCursor(c);
		mListView.invalidate();
	}

}
